<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="Active" prop="active">
        <el-input
          v-model="queryParams.active"
          placeholder="请输入Active"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="仓库" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置" prop="locationId">
        <el-input
          v-model="queryParams.locationId"
          placeholder="请输入位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入产品"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="最小数量" prop="productMinQty">
        <el-input
          v-model="queryParams.productMinQty"
          placeholder="请输入最小数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="最大数量" prop="productMaxQty">
        <el-input
          v-model="queryParams.productMaxQty"
          placeholder="请输入最大数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="数量倍数" prop="qtyMultiple">
        <el-input
          v-model="queryParams.qtyMultiple"
          placeholder="请输入数量倍数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="Procurement Group" prop="groupId">
        <el-input
          v-model="queryParams.groupId"
          placeholder="请输入Procurement Group"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="供应商名称" prop="companyId">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="前置时间" prop="leadDays">
        <el-input
          v-model="queryParams.leadDays"
          placeholder="请输入前置时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="创建者" prop="createUid">
        <el-input
          v-model="queryParams.createUid"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="最后更新者" prop="writeUid">
        <el-input
          v-model="queryParams.writeUid"
          placeholder="请输入最后更新者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="最后更新时间" prop="writeDate">
        <el-date-picker clearable
          v-model="queryParams.writeDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后更新时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:reorder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:reorder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:reorder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:reorder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <!-- <el-table-column label="Active" align="center" prop="active" /> -->
      <el-table-column label="仓库" align="center" prop="warehouseId" />
      <el-table-column label="位置" align="center" prop="locationId" />
      <el-table-column label="产品" align="center" prop="productId" />
      <el-table-column label="最小数量" align="center" prop="productMinQty" />
      <el-table-column label="最大数量" align="center" prop="productMaxQty" />
      <el-table-column label="数量倍数" align="center" prop="qtyMultiple" />
      <!-- <el-table-column label="Procurement Group" align="center" prop="groupId" /> -->
      <el-table-column label="供应商名称" align="center" prop="companyId" />
      <el-table-column label="前置时间" align="center" prop="leadDays" />
      <el-table-column label="Lead Type" align="center" prop="leadType" />
      <el-table-column label="创建者" align="center" prop="createUid" />
      <el-table-column label="创建日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新者" align="center" prop="writeUid" />
      <el-table-column label="最后更新时间" align="center" prop="writeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.writeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:reorder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:reorder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改重订货对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="id" prop="id">
          <el-input v-model="form.id" placeholder="请输入id" />
        </el-form-item> -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入Name" />
        </el-form-item>
        <el-form-item label="Active" prop="active">
          <el-input v-model="form.active" placeholder="请输入Active" />
        </el-form-item>
        <el-form-item label="仓库" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库" />
        </el-form-item>
        <el-form-item label="位置" prop="locationId">
          <el-input v-model="form.locationId" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="产品" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品" />
        </el-form-item>
        <el-form-item label="最小数量" prop="productMinQty">
          <el-input v-model="form.productMinQty" placeholder="请输入最小数量" />
        </el-form-item>
        <el-form-item label="最大数量" prop="productMaxQty">
          <el-input v-model="form.productMaxQty" placeholder="请输入最大数量" />
        </el-form-item>
        <el-form-item label="数量倍数" prop="qtyMultiple">
          <el-input v-model="form.qtyMultiple" placeholder="请输入数量倍数" />
        </el-form-item>
        <el-form-item label="Procurement Group" prop="groupId">
          <el-input v-model="form.groupId" placeholder="请输入Procurement Group" />
        </el-form-item>
        <el-form-item label="供货商名称" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="前置时间" prop="leadDays">
          <el-input v-model="form.leadDays" placeholder="请输入前置时间" />
        </el-form-item>
        <el-form-item label="创建者" prop="createUid">
          <el-input v-model="form.createUid" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后更新者" prop="writeUid">
          <el-input v-model="form.writeUid" placeholder="请输入最后更新者" />
        </el-form-item>
        <el-form-item label="最后更新时间" prop="writeDate">
          <el-date-picker clearable
            v-model="form.writeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReorder, getReorder, delReorder, addReorder, updateReorder } from "@/api/system/reorder";

export default {
  name: "Reorder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 重订货表格数据
      reorderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        active: null,
        warehouseId: null,
        locationId: null,
        productId: null,
        productMinQty: null,
        productMaxQty: null,
        qtyMultiple: null,
        groupId: null,
        companyId: null,
        leadDays: null,
        leadType: null,
        createUid: null,
        createDate: null,
        writeUid: null,
        writeDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "id不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "Name不能为空", trigger: "blur" }
        ],
        warehouseId: [
          { required: true, message: "Warehouse不能为空", trigger: "blur" }
        ],
        locationId: [
          { required: true, message: "Location不能为空", trigger: "blur" }
        ],
        productId: [
          { required: true, message: "Product不能为空", trigger: "blur" }
        ],
        productMinQty: [
          { required: true, message: "Minimum Quantity不能为空", trigger: "blur" }
        ],
        productMaxQty: [
          { required: true, message: "Maximum Quantity不能为空", trigger: "blur" }
        ],
        qtyMultiple: [
          { required: true, message: "Qty Multiple不能为空", trigger: "blur" }
        ],
        companyId: [
          { required: true, message: "Company不能为空", trigger: "blur" }
        ],
        leadType: [
          { required: true, message: "Lead Type不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询重订货列表 */
    getList() {
      this.loading = true;
      listReorder(this.queryParams).then(response => {
        this.reorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        active: null,
        warehouseId: null,
        locationId: null,
        productId: null,
        productMinQty: null,
        productMaxQty: null,
        qtyMultiple: null,
        groupId: null,
        companyId: null,
        leadDays: null,
        leadType: null,
        createUid: null,
        createDate: null,
        writeUid: null,
        writeDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加重订货";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReorder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改重订货";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReorder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReorder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除重订货编号为"' + ids + '"的数据项？').then(function() {
        return delReorder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/reorder/export', {
        ...this.queryParams
      }, `reorder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
